$(document).ready(function() {
  var grid_selector = "#grid-table";
  var pager_selector = "#grid-pager";
  //resize to fit page size
  $(window).on('resize.jqGrid', function() {
    $(grid_selector).jqGrid('setGridWidth', $(".page-content").width());
  })
  //resize on sidebar collapse/expand
  var parent_column = $(grid_selector).closest('[class*="col-"]');
  $(document).on('settings.ace.jqGrid', function(ev, event_name, collapsed) {
    if (event_name === 'sidebar_collapsed' || event_name === 'main_container_fixed') {
      //setTimeout is for webkit only to give time for DOM changes and then redraw!!!
      setTimeout(function() {
        $(grid_selector).jqGrid('setGridWidth', parent_column.width());
      }, 0);
    }
  })
  $(grid_selector).jqGrid({
    url : 'constant/getList.html',
    mtype : "POST",
    datatype : "json",
    colModel : [{
          label : '常量ID',
          name : 'constId',
          width : 60,
          sortable : false
        }, {
          label : '常量Key',
          name : 'constKey',
          width : 90,
          sortable : false
        }, {
          label : '常量名称',
          name : 'constName',
          width : 150,
          sortable : false
        }, {
          label : '常量值',
          name : 'constValue',
          width : 70,
          sortable : false
        }, {
          label : '常量说明',
          name : 'description',
          width : 90,
          sortable : false
        }, {
          label : '顺序',
          name : 'serialNumber',
          width : 150,
          sortable : false
        }, {
          label : '操作',
          width : 80,
          fixed : true,
          sortable : false,
          resize : false,
          formatter : 'actions',
          formatoptions : {
            keys : true,
            delOptions : {
              recreateForm : true,
              beforeShowForm : beforeDeleteCallback
            }
          }
        }],
    rownumbers : true,
    viewrecords : true,
    height : 380,
    rowNum : 10,
    rowList : [10, 20, 30],
    pager : pager_selector,
    altRows : true,
    loadComplete : function() {
      var table = this;
      setTimeout(function() {
        updatePagerIcons(table);
      }, 0);
    }
  }).navGrid(pager_selector, {
    edit : false,
    add : false,
    del : false,
    search : false,
    refresh : true,
    refreshicon : 'ace-icon fa fa-refresh green'
  });
  $(window).triggerHandler('resize.jqGrid');//trigger window resize to make the grid get the correct size
  //navButtons
  function style_delete_form(form) {
    var buttons = form.next().find('.EditButton .fm-button');
    buttons.addClass('btn btn-sm btn-white btn-round').find('[class*="-icon"]').hide();//ui-icon, s-icon
    buttons.eq(0).addClass('btn-danger').prepend('<i class="ace-icon fa fa-trash-o"></i>');
    buttons.eq(1).addClass('btn-default').prepend('<i class="ace-icon fa fa-times"></i>')
  }
  function beforeDeleteCallback(e) {
    var form = $(e[0]);
    if (form.data('styled'))
      return false;
    form.closest('.ui-jqdialog').find('.ui-jqdialog-titlebar').wrapInner('<div class="widget-header" />')
    style_delete_form(form);
    form.data('styled', true);
  }
  /**
   * 查询
   */
  $("#btn_search").click(function() {
    $("#grid-table").jqGrid("setGridParam", {
      postData : {
        constId : $.trim($('#constId').val()),
        constKey : $.trim($('#constKey').val()),
        constName : $.trim($('#constName').val())
      },
      datatype : 'json',
      page : 1
    }).trigger("reloadGrid");
  });
  /**
   * 新增
   */
  $("#btn_inster").click(function() {
  });
});
